package EBM.BLL;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import EBM.DB.UserDAO;
import EBM.model.User;

public class UserManage {
	private List<Object> tmp;
	private List<Object> response;
	private UserDAO userDAO;
	private boolean result; 

	//检查用户名是否存在
	public List<Object> CheckUserid(List<Object> request) throws SQLException, ClassNotFoundException{
		User user = new User();
		user.setUsrid(request.get(0).toString());

		userDAO = new UserDAO();
		tmp = userDAO.findByUserid(user.getUsrid());

		response = new ArrayList<Object>();

		if(tmp.size()>0)
			response.add("-2");
		else
			response.add("1");

		return response;
	}

	//检查用户名，密码
	public List<Object> CheckUser(List<Object> request) 
	throws SQLException, ClassNotFoundException{	
		User user = new User();
		user.setUsrid(request.get(0).toString());
		user.setPwd(request.get(1).toString());

		userDAO = new UserDAO();
		tmp = userDAO.findByUseridAndPwd(user.getUsrid(), user.getPwd());

		response = new ArrayList<Object>();

		if(tmp.size()>0)
			response.add(tmp.get(0));
		else
			response.add("-1"); 

		return response;
	}

	//修改密码
	public List<Object> ModifyPwd(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		User user = new User();
		user.setUsrid(request.get(0).toString());
		user.setPwd(request.get(1).toString());
		user.setNewpwd(request.get(2).toString());

		//验证当前用户的合法性
		response = CheckUser(request);
		if(response.get(0).equals("-1")){
			return response;
		}else{
			response.clear();
		}

		userDAO = new UserDAO();
		result = userDAO.updatePwd(user.getNewpwd(),user.getUsrid());

		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}

	//增加用户
	public List<Object> AddUsr(List<Object> request) 
	throws SQLException, ClassNotFoundException{
		//检查用户名是否存在
		response = CheckUserid(request);
		if(response.get(0).equals("-2")){
			return response;
		}else{
			response.clear();
		}

		User user = new User();
		user.setUsrid(request.get(0).toString());
		user.setName(request.get(1).toString());
		user.setType_usr(request.get(2).toString());

		userDAO = new UserDAO();
		result = userDAO.save(user);

		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}

	//删除用户信息
	public List<Object> DeleteUser(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		User user = new User();
		user.setUsrid(request.get(0).toString());

		userDAO = new UserDAO();
		result = userDAO.delete(user);

		response = new ArrayList<Object>();
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//修改用户信息
	public List<Object> FixUsr(List<Object> request) 
	throws SQLException, ClassNotFoundException{

		User user = new User();
		user.setUsrid(request.get(0).toString());
		user.setName(request.get(1).toString());
		user.setType_usr(request.get(2).toString());

		userDAO = new UserDAO();
		result = userDAO.update(user);

		response = new ArrayList<Object>();
		
		if(result){
			response.add("1");
		}else{
			response.add("-1");
		}
		return response;
	}
	
	//查找所有用户
	public List<Object> GetAllUser() 
	throws SQLException, ClassNotFoundException{

		userDAO = new UserDAO();	
		response = userDAO.findAll();

		//当前没有用户
		if(response.size()<1){
			response.add("-1");
		}

		return response;
	}
	
	//查找所有用户
	public List<Object> FindUserList(List<Object> request)
	throws SQLException, ClassNotFoundException{
		
		User user = new User();
		String proid = request.get(0).toString();
		user.setUsrid(request.get(1).toString());
		user.setName(request.get(2).toString());
		user.setType_usr(request.get(3).toString());
	    
		userDAO = new UserDAO();	
		tmp = userDAO.findByCertainProperty(proid, user);
		response = new ArrayList<Object>();
		
		for(Iterator<Object> it = tmp.iterator();it.hasNext();){
			User u = new User();
			u = (User)it.next();
			response.add(u.getUsrid());
			response.add(u.getName());
			response.add(u.getType_usr());
		}
		
		//没有符合条件的用户
		if(tmp.size()<1){
			response.add("-1");
		}

		return response;
	}

	//根据用户名查找用户的id
	public String GetId(String userId) 
	throws SQLException, ClassNotFoundException{

		userDAO = new UserDAO();	
		User user = userDAO.findByUseridNew(userId);

		if(user==null)
			return "-1";
		else		
			return user.getId();
	}

}
